<template>
    <el-dialog
      title="选择数据"
      :visible.sync="dialogVisible"
      width="700px"
      :modal="false"
      @close="handleClose"
    >
      <!-- 查询条件 -->
      <el-form :inline="true" @submit.native.prevent="handleSearch">
        <el-form-item label="名称">
          <el-input v-model="queryParams.name" placeholder="请输入名称"></el-input>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="handleSearch">查询</el-button>
        </el-form-item>
      </el-form>
  
      <!-- 数据表格 -->
      <el-table
        :data="tableData"
        @row-click="handleRowClick"
        style="width: 100%"
        highlight-current-row
      >
        <el-table-column prop="projectId" label="编码" width="220"></el-table-column>
        <el-table-column prop="projectName" label="名称"></el-table-column>
      </el-table>
  
      <!-- 分页 -->
      <el-pagination
        style="margin-top: 20px; text-align: right;"
        :current-page="queryParams.pageNum"
        :page-size="queryParams.pageSize"
        :total="total"
        @current-change="handlePageChange"
        layout="total, prev, pager, next"
      ></el-pagination>
  
      <!-- 弹窗底部 -->
      <span slot="footer" class="dialog-footer">
        <el-button @click="dialogVisible = false">取消</el-button>
        <el-button type="primary" @click="handleConfirm">确认</el-button>
      </span>
    </el-dialog>
  </template>
  
  <script>
  import { listProject } from "@/api/pms/project"; // 假设这是后台接口
  
  export default {
    name: "RemoteSelectDialog",
    props: {
      visible: {
        type: Boolean,
        default: false,
      },
    },
    data() {
      return {
        dialogVisible: this.visible, // 控制弹窗显示
        queryParams: {
          name: "", // 查询条件
          pageNum: 1, // 当前页码
          pageSize: 10, // 每页条数
        },
        tableData: [], // 表格数据
        total: 0, // 总条数
        selectedRow: null, // 选中的行数据
      };
    },
    watch: {
      visible(newVal) {
        this.dialogVisible = newVal;
        if (newVal) {
          this.loadData(); // 打开弹窗时加载数据
        }
      },
    },
    methods: {
      // 加载数据
      async loadData() {
        try {
          const response = await listProject(this.queryParams);
          this.tableData = response.rows;
          this.total = response.total;
        } catch (error) {
          console.error("加载数据失败：", error);
        }
      },
      // 查询
      handleSearch() {
        this.queryParams.pageNum = 1; // 重置页码
        this.loadData();
      },
      // 分页切换
      handlePageChange(pageNum) {
        this.queryParams.pageNum = pageNum;
        this.loadData();
      },
      // 行点击事件
      handleRowClick(row) {
        this.selectedRow = row;
      },
      // 确认选择
      handleConfirm() {
        if (this.selectedRow) {
          this.$emit("confirm", this.selectedRow);
          this.dialogVisible = false;
        } else {
          this.$message.warning("请选择一行数据");
        }
      },
      // 关闭弹窗
      handleClose() {
        this.$emit("close");
      },
    },
  };
  </script>